from django.db import models
from datetime import datetime


class PowerData(models.Model):
    sn_id = models.TextField(null=False, default="", verbose_name="sn")
    # 电网电压
    grid_voltage = models.DecimalField(max_digits=10, decimal_places=3)

    # 电网电压谐波总畸变率
    grid_voltage_harmonic_distortion = models.DecimalField(max_digits=10, decimal_places=3)

    # 电网频率
    grid_frequency = models.DecimalField(max_digits=10, decimal_places=3)

    # 输出电压
    output_voltage = models.DecimalField(max_digits=10, decimal_places=3)

    # 电网(输出)电流
    grid_current = models.DecimalField(max_digits=10, decimal_places=3)

    # 并网(输出)有功功率
    grid_active_power = models.DecimalField(max_digits=10, decimal_places=3)

    # 并网(输出)无功功率
    grid_reactive_power = models.DecimalField(max_digits=10, decimal_places=3)

    # 并网(输出)视在功率
    grid_apparent_power = models.DecimalField(max_digits=10, decimal_places=3)

    # 并网(设备)功率因数
    grid_power_factor = models.DecimalField(max_digits=10, decimal_places=3)

    # UPS(负载)电压
    ups_voltage = models.DecimalField(max_digits=10, decimal_places=3)

    # UPS(负载)电流
    ups_current = models.DecimalField(max_digits=10, decimal_places=3)

    # UPS(负载)频率
    ups_frequency = models.DecimalField(max_digits=10, decimal_places=3)

    # UPS(负载)有功功率
    ups_active_power = models.DecimalField(max_digits=10, decimal_places=3)

    # UPS(负载)无功功率
    ups_reactive_power = models.DecimalField(max_digits=10, decimal_places=3)

    # UPS(负载)视在功率
    ups_apparent_power = models.DecimalField(max_digits=10, decimal_places=3)

    # UPS(负载)功率因数
    ups_power_factor = models.DecimalField(max_digits=10, decimal_places=3)

    # BUS电压
    bus_voltage = models.DecimalField(max_digits=10, decimal_places=3)

    create_time = models.DateTimeField(null=True, blank=True, default=datetime.now, verbose_name="创建时间")

    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        app_label = 'app'


class BMSInformation(models.Model):
    sn_id = models.TextField(null=False, default="", verbose_name="sn")
    # 总电压
    total_voltage = models.DecimalField(max_digits=10, decimal_places=3)

    # 总电流
    total_current = models.DecimalField(max_digits=10, decimal_places=3)

    # 充放电状态
    charge_discharge_status = models.IntegerField()

    # 系统状态
    system_status = models.IntegerField()

    # 建议充电电压
    recommended_charge_voltage = models.DecimalField(max_digits=10, decimal_places=3)

    # 建议放电电压
    recommended_discharge_voltage = models.DecimalField(max_digits=10, decimal_places=3)

    # 最大允许充电电流
    max_charge_current = models.DecimalField(max_digits=10, decimal_places=3)

    # 最大允许放电电流
    max_discharge_current = models.DecimalField(max_digits=10, decimal_places=3)

    # SOC（State of Charge）
    soc = models.DecimalField(max_digits=10, decimal_places=3)

    # SOH（State of Health）
    soh = models.DecimalField(max_digits=10, decimal_places=3)

    # 温度
    temperature = models.DecimalField(max_digits=10, decimal_places=3)

    # 系统故障状态
    system_fault_status = models.IntegerField()

    # 充电可用电量
    charge_available_energy = models.DecimalField(max_digits=10, decimal_places=3)

    # 放电可用电量
    discharge_available_energy = models.DecimalField(max_digits=10, decimal_places=3)

    # 告警信息字1
    alarm_info_1 = models.IntegerField()

    # 告警信息字2
    alarm_info_2 = models.IntegerField()

    # 告警信息字3
    alarm_info_3 = models.IntegerField()

    # 告警信息字4
    alarm_info_4 = models.IntegerField()

    # BMS版本
    bms_version = models.CharField(max_length=255)

    # 厂商信息1
    manufacturer_info_1 = models.CharField(max_length=255)

    # 厂商信息2
    manufacturer_info_2 = models.CharField(max_length=255)

    # 厂商信息3
    manufacturer_info_3 = models.CharField(max_length=255)

    create_time = models.DateTimeField(null=True, blank=True, default=datetime.now, verbose_name="创建时间")

    update_time = models.DateTimeField(null=True, blank=True, default=datetime.now, verbose_name="更新时间")

    class Meta:
        app_label = 'app'

    def __str__(self):
        return f"BMS Information {self.id}"


class BoatBatteryInfo(models.Model):
    # bms SN
    bms_sn = models.TextField(null=False, default="", verbose_name="sn")
    # PV1

    bms_soc = models.DecimalField(max_digits=10, decimal_places=3)
    bms_soh = models.DecimalField(max_digits=10, decimal_places=3)
    # 剩余电量
    remaining_battery = models.DecimalField(max_digits=10, decimal_places=3)
    # 总电压
    total_voltage = models.DecimalField(max_digits=10, decimal_places=3)

    # 额定电压
    rated_voltage = models.DecimalField(max_digits=10, decimal_places=3)
    # 电流
    current = models.DecimalField(max_digits=10, decimal_places=3)

    # 最大电流
    max_current = models.DecimalField(max_digits=10, decimal_places=3)

    # 最小温度
    min_temp = models.DecimalField(max_digits=10, decimal_places=3)

    # 最大温度
    max_temp = models.DecimalField(max_digits=10, decimal_places=3)

    # 最小电压
    min_voltage = models.DecimalField(max_digits=10, decimal_places=3)

    # 最大电压
    max_voltage = models.DecimalField(max_digits=10, decimal_places=3)

    # 簇数
    cluster_count = models.IntegerField()

    create_time = models.DateTimeField(null=True, blank=True, default=datetime.now, verbose_name="创建时间")

    update_time = models.DateTimeField(null=True, blank=True, default=datetime.now, verbose_name="更新时间")


class BoatSingleBattery(models.Model):
    bms_sn = models.TextField(null=False, default="", verbose_name="sn")

    # 簇号
    cluster_id = models.IntegerField()

    # 电池编号
    battery_num = models.IntegerField()

    # 单体电压
    battery_voltage = models.DecimalField(max_digits=10, decimal_places=3)

    # 单体温度
    battery_temp = models.DecimalField(max_digits=10, decimal_places=3)

    create_time = models.DateTimeField(null=True, blank=True, default=datetime.now, verbose_name="创建时间")

    update_time = models.DateTimeField(null=True, blank=True, default=datetime.now, verbose_name="更新时间")

    class Meta:
        app_label = "app"
